<!-- 某个合同绑定的水表列表明细 -->
<!-- 能源费 -->
<template>
	<div class="zs-table-page-container">
		<div class="zs-card-sm zs-space-bottom zs-layout-flex-row zs-layout-justify-between">
			<span>
				<el-input v-model="queryParams.deviceNum" class="zs-select-md zs-space-right-sm" placeholder="编号" disabled />
				<el-input v-model="queryParams.deviceName" class="zs-select-md zs-space-right-sm" placeholder="水表名称" disabled />
			</span>
		</div>

		<!-- teble -->
		<div class="zs-card zs-table-page-content">
			<el-table class="zs-table-page-main" :data="tableData.data" v-loading="tableData.loading" stripe>
				<el-table-column type="index" label="序号" width="60" align="center" :index="(index) => index + 1 + (tableData.currentPage - 1) * curPageSize" />
				<el-table-column prop="merchantName" label="商户名称/设施名称" minWidth="120" show-overflow-tooltip align="center" :formatter="defaultTableData" />
				<el-table-column prop="operationName" label="操作" minWidth="120" show-overflow-tooltip align="center" :formatter="defaultTableData" />
				<el-table-column prop="formatOperationTime" label="操作时间" minWidth="120" show-overflow-tooltip align="center" :formatter="defaultTableData" />
				<el-table-column prop="operator" label="操作人" minWidth="120" align="center" :formatter="defaultTableData" />
				<el-table-column prop="reading" label="读数" minWidth="120" align="center" :formatter="defaultTableData" />
			</el-table>
			<div class="zs-space-top-sm zs-table-page-pagination">
				<el-pagination v-model:currentPage="tableData.currentPage" v-model:page-size="curPageSize" :page-sizes="curPageSizes" :small="true" :background="true" layout="total, sizes, prev, pager, next, jumper" :total="tableData.total" @size-change="pageChange('size', $event)" @current-change="pageChange('page', $event)" />
			</div>
		</div>
	</div>
</template>

<script setup>
	import { waterMeterRecordListApi, electricityMeterRecordListApi } from '@/api/energy.js';
	import { equipmentInfoApi } from '@/api/equipment.js';
	import { ref, reactive } from 'vue';
	import { useStore } from 'vuex';
	import { useRoute } from 'vue-router';
	import { defaultTableData, parseTime } from '@/utils/publicUtils';

	const route = useRoute();
	const store = useStore();
	const curPageSizes = ref(store.state.uiPageSizes);
	const curPageSize = ref(store.state.uiPageSizeDefault);

	// 列表 filed
	let tableData = reactive({
		currentPage: 1,
		total: 0,
		loading: false,
		data: [],
	});

	//查询条件
	let queryParams = reactive({ waterMeterId: route.query.id, electricMeterId: route.query.id, pageNumber: 1, pageSize: curPageSize.value });

	// 列表数据源
	const queryList = async init => {
		tableData.loading = true;
		let res = route.query.dType == "water" ? await waterMeterRecordListApi(queryParams) : await electricityMeterRecordListApi(queryParams);
		if (res.data.code !== 200) return;
		initData(res.data.data.list);
		tableData.data = res.data.data.list || [];
		tableData.total = res.data.data.total || 0;
		tableData.loading = false;
	};

	const initData = list => {
		list.forEach(item => {
			item.formatOperationTime = parseTime(item.operationTime, '{y}-{m}-{d}');
			item.operationName = item.operation == 1 ? "挂表" : "解表";
		});
	};

	// 分页
	const pageChange = (type, size) => {
		if (type == 'size') {
			queryParams.pageSize = size;
		} else {
			queryParams.pageNumber = size;
		}
		queryList('init');
	};

    //设备详情 
	const getBaseInfo = async () => {
		if (!route.query.deviceId) return;
		let res = await equipmentInfoApi(route.query.deviceId);
		if (res.data.code !== 200) return;
		queryParams.deviceNum = res.data.data.deviceNum;
		queryParams.deviceName = res.data.data.deviceName;
	};

    getBaseInfo()
	queryList();
</script>
